$(document).ready(function(){
$('#submit').live('click', function() {
	
	//console.log($('#geoform').serialize());
	//$('#waiter').show();
	
	 $('#debug').empty();
	  
	  $('#notification').css('visibility','visible');
	
	var prop_id = $('#propertyid').val();
	var color = $('#color').val();
	var lat = $('#lat').val();
	var lon = $('#lon').val();
	var token = $('input[name=bmPis2012Tok]').val();
	
	
	//var lat_error=$('#lat_error').val();
	//var lat_error="Please enter correct latitude";
	//var lon_error=$('#lon_error').val();
	
	//var error1=$('#error1').val();
	
		//fire up ajax
		$.ajax({
	        type: "POST",
	        url: baseurl+"insertdata/addgeo",
	        dataType: "json",
	        data: "propertyid="+prop_id+"&color="+color+"&lat="+lat+"&lon="+lon+"&bmPis2012Tok="+token,
	        //error:"error1="+error1,
	       // data:form_data,
	        cache:false,
	        success: 
	          function(data){
	  
	        	if(data.status=='success'){
	            	$("#debug").append('Your Data is successfully updated');
	            	$('#mygeo_Modal').reveal({
					     animation: 'fadeAndPop',                   //fade, fadeAndPop, none
					     animationspeed: 300,                       //how fast animtions are
					     closeonbackgroundclick: false,              //if you click background will modal close?
					     dismissmodalclass: 'close-reveal-modal'    //the class of a button or element that will close an open modal
				  });
	            	setTimeout(function() {
	  				  $("#close-reveal-modal").trigger("click");
	  				  $('#notification').css('visibility','hidden');
	  				  window.location.href = baseurl+"insertdata/addgeo";
	  				}, 3000);
	        		
	        		
	        	}
	        	else if(data.propertyid){
	        		//$(#propertyid_error).show();
	        		$('#debug').append("There are no properties without having the details of GEO location data");
	        		$('#mygeo_Modal').reveal({
					     animation: 'fadeAndPop',                   //fade, fadeAndPop, none
					     animationspeed: 300,                       //how fast animtions are
					     closeonbackgroundclick: false,              //if you click background will modal close?
					     dismissmodalclass: 'close-reveal-modal'    //the class of a button or element that will close an open modal
				  });
	        		$("#close-reveal-modal").click(function(){
	  				  $('#notification').css('visibility','hidden');
	  			  });
	        		
	        		setTimeout(function() {
		  				  $("#close-reveal-modal").trigger("click");
		  				  $('#notification').css('visibility','hidden');
		  				  window.location.href = baseurl+"insertdata/addgeo";
		  				}, 3000);
	        		
	        	}
	        	else if(data.status=='color_exist'){
	        		$('#debug').append("The color you selected is already exsits. Please select another color");
	        		$('#mygeo_Modal').reveal({
					     animation: 'fadeAndPop',                   //fade, fadeAndPop, none
					     animationspeed: 300,                       //how fast animtions are
					     closeonbackgroundclick: false,              //if you click background will modal close?
					     dismissmodalclass: 'close-reveal-modal'    //the class of a button or element that will close an open modal
				  });
	        		$("#close-reveal-modal").click(function(){
	  				  $('#notification').css('visibility','hidden');
	  			  });
	        	}
	        	else {
	        		$('#debug').append("Please Enter Both Latitude & Longitude");
	        		$('#mygeo_Modal').reveal({
					     animation: 'fadeAndPop',                   //fade, fadeAndPop, none
					     animationspeed: 300,                       //how fast animtions are
					     closeonbackgroundclick: false,              //if you click background will modal close?
					     dismissmodalclass: 'close-reveal-modal'    //the class of a button or element that will close an open modal
				  });
	        		$("#close-reveal-modal").click(function(){
	  				  $('#notification').css('visibility','hidden');
	  			  });
	        		
	        	}
	        	
	          }
	        
	        });
		
		return false;
	});
});

